package com.gsit.misn.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import com.gsit.misn.domain.Patient;
import com.gsit.misn.domain.User;

import static  com.gsit.misn.utils.Constant.BLANK;

public class ExcelReader implements Reader<User, List<Patient>> {
	private InputStream inputStream = null;

	public ExcelReader() {

	}

	@Override
	public List<Patient> read(User user) {
		List<Patient> patients = new ArrayList<Patient>();
		Boolean isFirstRecord = Boolean.TRUE;
		int noOfRecord = 0; 
		try {
			inputStream = new FileInputStream(Path.xlsPath + user.getXlsName());
			POIFSFileSystem fileSystem = null;

			fileSystem = new POIFSFileSystem(inputStream);

			HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
			HSSFSheet sheet = workBook.getSheetAt(0);
			Iterator<Row> rows = sheet.rowIterator();
			
			

			while (rows.hasNext()) {
				Row row = rows.next();
				if(noOfRecord == user.getTotalNoOfPages().intValue() ){
					break;
				}
				if(isFirstRecord){
					isFirstRecord = Boolean.FALSE;
				}else{
					patients.add(createPatient(row));
				}
				
				noOfRecord++;
				
			}

		} catch (IOException e) {
			System.out.println("File not found in the specified path.");
			e.printStackTrace();
		}
		
		/*for (Patient patient : patients) {
			System.out.println("The ps is "+patient.toString());
		}*/
		
		return patients;
	}

	private Patient createPatient(Row row) {

	
		Patient patient = new Patient();
		
		System.out.println("Row No.: " + row.getRowNum());
		
		
		for(int colIndex = 0; colIndex < 18; colIndex++) {
			Cell cell = row.getCell(colIndex); 
			String celValue = "";
			if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
				 celValue = cell.getNumericCellValue()+"";
			}else{
				 celValue = (BLANK.equalsIgnoreCase(cell.getStringCellValue())) ?"" :cell.getStringCellValue();
			}
		
			
			celValue = celValue.trim();
			
			
			switch (colIndex) {

			case 0:
				patient.setId(celValue);
				break;
			case 1:
				patient.setPatientID(celValue);
				break;
			case 2:
				patient.setPatientName(celValue);
				break;
			case 3:
				patient.setDoctorName(celValue);
				break;
			case 4:
				patient.setRelativeName(celValue);
				break;
			case 5:
				patient.setAddress(celValue);
				break;
			case 6:
				patient.setCity(celValue);
				break;
			case 7:
				patient.setState(celValue);
				break;
			case 8:
				patient.setZip(celValue);
				break;
			case 9:
				patient.setCountry(celValue);
				break;
			case 10:
				patient.setPhone(celValue);
				break;
			case 11:
				patient.setEmail(celValue);
				break;
			case 12:
				patient.setGender(celValue);
				break;
			case 13:
				patient.setDateofBirth(celValue);
				break;				
			case 14:
				patient.setHeight(celValue);
				break;
			case 15:
				patient.setWeight(celValue);
				break;
			case 16:
				patient.setOccupation(celValue);
				break;
			case 17:
				patient.setMaritalStatus(celValue);
				break;
			case 18:
				patient.setEditURL(celValue);
				break;

			}
		}
		
		System.out.println("The ps detaIil is :  "+patient.toString());
		
		return patient;

	}
	
	
	private Patient createPatientOld(Row row) {

		Iterator<Cell> cells = row.cellIterator();
		int pos = -1;
		Patient patient = new Patient();
		
		System.out.println("Row No.: " + row.getRowNum());
		
		while (cells.hasNext()) {
			Cell cell = cells.next();
			pos++;
			String celValue = "";
			if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
				 celValue = cell.getNumericCellValue()+"";
			}else{
				 celValue = (BLANK.equalsIgnoreCase(cell.getStringCellValue())) ?"" :cell.getStringCellValue();
			}
			
		
			
			switch (pos) {

			case 0:
				patient.setId(celValue);
				break;
			case 1:
				patient.setPatientID(celValue);
				break;
			case 2:
				patient.setPatientName(celValue);
				break;
			case 3:
				patient.setDoctorName(celValue);
				break;
			case 4:
				patient.setRelativeName(celValue);
				break;
			case 5:
				patient.setAddress(celValue);
				break;
			case 6:
				patient.setCity(celValue);
				break;
			case 7:
				patient.setState(celValue);
				break;
			case 8:
				patient.setZip(celValue);
				break;
			case 9:
				patient.setCountry(celValue);
				break;
			case 10:
				patient.setPhone(celValue);
				break;
			case 11:
				patient.setEmail(celValue);
				break;
			case 12:
				patient.setGender(celValue);
				break;
			case 13:
				patient.setDateofBirth(celValue);
				break;				
			case 14:
				patient.setHeight(celValue);
				break;
			case 15:
				patient.setWeight(celValue);
				break;
			case 16:
				patient.setOccupation(celValue);
				break;
			case 17:
				patient.setMaritalStatus(celValue);
				break;
			case 18:
				patient.setEditURL(celValue);
				break;

			}

		}
		
		System.out.println("The ps detaIil is :  "+patient.toString());
		
		return patient;

	}



}
